/**
 * @author LKQ
 * @date 2022/1/8 13:46
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {-2,-3,-1};
        System.out.println(solution.maxSubarraySumCircular(nums));
    }

    public int maxSubarraySumCircular(int[] nums) {
        int n = nums.length;
        int[] arr = new int[2*n];
        for (int i = 0; i < 2 * n; i++) {
            arr[i] = nums[i % n];
        }
        int ans = Integer.MIN_VALUE;
        for (int i = 0; i < n; i++) {
            // 超时
            ans = Math.max(ans, help(arr, i, i + n-1));
        }
        return ans;
    }
    public int help(int[] arr, int start, int end) {
        int ans = Integer.MIN_VALUE, pre = 0;
        for (int i = start; i<=end; i++) {
            pre = Math.max(pre + arr[i], arr[i]);
            ans = Math.max(pre, ans);
        }
        return ans;
    }
}
